package com.platform.server.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.platform.server.service.VipSettingService;

@Controller
public class VipSettingController {

	@Autowired
	private VipSettingService service;
	
	/**
	 * 修改or添加个人、企业年费
	 * @throws IOException 
	 * @throws UnsupportedEncodingException 
	 * @throws  
	 */
	@RequestMapping("/OrVipSettingAddUpdate")
	public void setCommontDiscount(HttpServletResponse response,String id1,String id2,String id3,String is_auth1,String is_auth2,String is_auth3,String money1,String money2,String money3) throws Exception {
		int flag=0;
		SimpleDateFormat fdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> selectSetting = service.selectSetting(id1, is_auth1, null, null, null);
		if(selectSetting.size()>0){
			 flag = service.updateVipSetting(Integer.parseInt(id1), null, null, fdt.format(new Date()), money1);
			 if(flag!=1){
				 flag=0; 
			 }
		}else{
			 flag = service.insertVipSetting(1, "1", fdt.format(new Date()), fdt.format(new Date()), money1);
			 if(flag!=1){
				 flag=0; 
			 }
		}
		List<Map<String, Object>> selectSetting2 = service.selectSetting(id2, is_auth2, null, null, null);
		if(selectSetting2.size()>0){
			 flag = service.updateVipSetting(Integer.parseInt(id2), null, null, fdt.format(new Date()), money2);
			 if(flag!=1){
				 flag=0; 
			 }
		}else{
			 flag = service.insertVipSetting(2, "2", fdt.format(new Date()), fdt.format(new Date()), money1);
			 if(flag!=1){
				 flag=0; 
			 }
		}
		List<Map<String, Object>> selectSetting3 = service.selectSetting(id3, is_auth3, null, null, null);
		if(selectSetting3.size()>0){
			 flag = service.updateVipSetting(Integer.parseInt(id3), null, null, fdt.format(new Date()), money3);
			 if(flag!=1){
				 flag=0; 
			 }
		}else{
			 flag = service.insertVipSetting(3, "3", fdt.format(new Date()), fdt.format(new Date()), money1);
			 if(flag!=1){
				 flag=0; 
			 }
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag",flag);
		response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
}
